package com.example.baidu.speech.realtime.components;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;

@Component
public class SpringContext {

    /**
     * 自定义 context，用来缓存 Spring 容器中的 applicationContext
     */
    private static ApplicationContext context;

    @Autowired
    private ApplicationContext applicationContext;

    @PostConstruct
    public void init() {
        context = applicationContext;
    }

    /**
     * 从容器当中获取实例
     */
    public static <T> T getBean(Class<T> clazz) {
        return context.getBean(clazz);
    }
}
